package com.lzhl.buildjar.util;

import com.lzhl.buildjar.dao.DaoFactory;
import com.lzhl.buildjar.exception.AbortException;
import com.lzhl.buildjar.po.Category;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * @author 李柱良(@柱子的日月)
 * @date 2016/2/27
 */
public class UIUtils {

    public static void abort() {
        throw new AbortException();
    }

    public static void abort(String message) {
        throw new AbortException(message);
    }

    public static void abort(String message, Runnable after) {
        throw new AbortException(message, after);
    }

    public static String getCategoryDir(long id) {
        Category tree = DaoFactory.getCategoryDao().getCategoryTree(id);
        String output = "";
        while(true) {
            if(tree.equals(Category.ROOT)) {
                return output;
            }
            output = tree.getName() + File.separator + output;
            tree = tree.getParent();
            if(tree == null) {
                break;
            }
        }
        return output;
    }

    public static void invoke(Runnable runnable) {
        new Thread(runnable).start();
    }

    public static void showOwnerFocus(final Window window) {
        new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            System.out.println(window.getFocusOwner());
                        }
                    });
                }
            }
        }).start();
    }

}
